package com.zl_huang.example.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zl_huang.example.dao.IUserDaoHibernate;
import com.zl_huang.example.model.User;
import com.zl_huang.example.model.vo.UserVO;
import com.zl_huang.example.util.ExcelUtils;
@Service
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDaoHibernate userDaoHibernate;

	@Override
	public List<User> findAllUsers() throws Exception {
		return userDaoHibernate.findAllUsers();
		
	}

	@Override
	public Integer createUser(User user) throws Exception {
		
		return userDaoHibernate.createUser(user);
		
	}

	@Override
	public User getUserById(String user_id) throws Exception {
		return userDaoHibernate.getUserById(user_id);
	}

	@Override
	public Integer editUser(User user) throws Exception {
		return userDaoHibernate.editUser(user);
		
	}

	@Override
	public Integer deleteUser(String user_id) throws Exception {
		return userDaoHibernate.deleteUser(user_id);
		
	}
	@Override
	public void saveStudentByExcel(InputStream inputStream,String postfix){
		Workbook wk = ExcelUtils.getWorkbook(inputStream, postfix);
		List<UserVO> allUserVO = ExcelUtils.getStudentListByExcel(wk);
		List<User> allUser = new ArrayList<User>();
		if(allUserVO != null){
			User user = null;
			for(int i=0;i < allUserVO.size();i++){
				user = new User();
				BeanUtils.copyProperties(allUserVO.get(i), user);				
				allUser.add(user);
			}
		}
		userDaoHibernate.save(allUser);
	}

}
